package com.nest.WorkNest.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nest.WorkNest.R;
import com.nest.WorkNest.adapter.ProductSelectAdapter;
import com.nest.WorkNest.tools.ParseUtils;
import com.nest.WorkNest.view.ListViewForScrollView;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * Created by only1 on 2015/8/23 0023.
 */
public class ProductSelectAct extends BaseActivity implements OnClickListener {

    ListViewForScrollView lvProductdetailSelect;
    @Bind(R.id.btn_productselect_submit)
    Button btnProductselectSubmit;
    private LayoutInflater layoutInflater;
    private Context context = ProductSelectAct.this;
    private String[] color;
    private String[] size;
    private ArrayList<HashMap<String, String>> dataList = new ArrayList<>();
    private ProductSelectAdapter productSelectAdapter;
    private ArrayList<HashMap<String, String>> getdataList = new ArrayList<>();
    private Intent mIntent;
    private int totalNumber = 0;
    private int minOrderNumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_product_select);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mIntent = getIntent();
        initBar();
        if (mIntent.getSerializableExtra("getdatalist") == null) {
            initData();
        } else {
            this.dataList = (ArrayList<HashMap<String, String>>) mIntent.getSerializableExtra("getdatalist");
        }
        manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        actionbar_side_name.setVisibility(View.VISIBLE);
        actionbar_side_name.setText("选择款式数量");
        ll_iv_actionbar_side_left.setVisibility(View.VISIBLE);
        back();
        layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View headView = layoutInflater.inflate(R.layout.itemhead_orderedit, null);
        productSelectAdapter = new ProductSelectAdapter(context, dataList);
        lvProductdetailSelect = (ListViewForScrollView) findViewById(R.id.lv_productdetail_select);
        lvProductdetailSelect.setFocusable(false);
        lvProductdetailSelect.setSelector(R.color.bg_transparent);
        lvProductdetailSelect.addHeaderView(headView);
        lvProductdetailSelect.setAdapter(productSelectAdapter);
        btnProductselectSubmit.setOnClickListener(this);
        this.minOrderNumber = mIntent.getIntExtra("minOrderNumber", 0);
        productSelectAdapter.update(dataList);

    }


    private void initData() {
        Bundle bundle = this.getIntent().getExtras();
        color = bundle.getStringArray("color");
        size = bundle.getStringArray("size");
        dataList.clear();
        for (int i = 0; i < color.length; i++) {
            for (int j = 0; j < size.length; j++) {
                HashMap<String, String> data = new HashMap<>();
                data.put("style", color[i] + "/" + size[j]);
                dataList.add(data);
            }
        }

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_productselect_submit:
                for (int i = 1; i < lvProductdetailSelect.getCount(); i++) {
                    HashMap<String, String> getdata = new HashMap<>();
                    LinearLayout linearLayout = (LinearLayout) lvProductdetailSelect.getAdapter().getView(i, null, null);
                    EditText editText = (EditText) lvProductdetailSelect.getChildAt(i).findViewById(R.id.edtTxt_orderedit_number);
                    TextView textView = (TextView) linearLayout.findViewById(R.id.tv_orderedit_style);
                    String style = textView.getText().toString();
                    String[] styles = style.split("/");
                    String color = styles[0];
                    String size = styles[1];
                    String number = editText.getText().toString();
                    getdata.put("color", color);
                    getdata.put("size", size);
                    getdata.put("number", number);
                    getdataList.add(getdata);
                }
                for (int j = 0; j < getdataList.size(); j++) {
                    totalNumber += ParseUtils.string2int(getdataList.get(j).get("number"));
                }
                if (totalNumber >= minOrderNumber) {
                    Intent intent = new Intent();
                    intent.putExtra("getdatalist", getdataList);
                    intent.putExtra("all", totalNumber);
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    showToast("您选择的数量低于起批数");
                }

                break;
        }
    }

    public boolean onTouchEvent(MotionEvent event) {
        try {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
                    manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return super.onTouchEvent(event);
    }
}
